package com.uzhie.media.spring;

import com.uzhie.tcpDispatch.*;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by TangFei on 2018/8/9.
 * 方法映射加载
 */
public class ApplicationContextAware implements org.springframework.context.ApplicationContextAware {

    private Logger logger = Logger.getLogger(this.getClass());

    private MethodInvoke methodInvoke;

    public void setMethodInvoke(MethodInvoke methodInvoke) {
        this.methodInvoke = methodInvoke;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.info("开始加载 流媒体 请求处理方法");
        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(MsgService.class);
        for (String key : beansWithAnnotation.keySet()) {
            for (Method method : beansWithAnnotation.get(key).getClass().getMethods()) {
                if (method.isAnnotationPresent(MsgIDMapping.class)) {
                    logger.info("mapping msgID [" + method.getAnnotation(MsgIDMapping.class).msgID() + "] on method " + method);
                    methodInvoke.put(method.getAnnotation(MsgIDMapping.class).msgID(), new ClassMethod(beansWithAnnotation.get(key), method));
                }
            }
        }
        logger.info("加载 流媒体 请求处理方法完成");
    }

}
